package com.bst.md.service.impl;

import com.bst.common.base.service.IBaseDWService;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.core.text.Convert;
import com.bst.md.domain.MdMedDim;
import com.bst.md.mapper.MdMedDimMapper;
import com.bst.md.service.IMdMedDimService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 维度管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-07
 */
@Service
public class MdMedDimServiceImpl implements IMdMedDimService
{
    @Autowired
    private MdMedDimMapper mdMedDimMapper;

    /**
     * 查询维度管理
     * 
     * @param idDim 维度管理主键
     * @return 维度管理
     */
    @Override
    public MdMedDim selectMdMedDimByIdDim(Long idDim)
    {
        return mdMedDimMapper.selectMdMedDimByIdDim(idDim);
    }

    @Override
    public MdMedDim selectMdMedDimByTableName(String tableName) {
        MdMedDim dim = new MdMedDim();
        dim.setCdTb(tableName);
        List<MdMedDim> dims = this.selectMdMedDimList(dim);
        if(dims.size()>0) {
            dim = dims.get(0);
        } else {
            dim = null;
        }
        return dim;
    }

    /**
     * 查询维度管理列表
     * 
     * @param mdMedDim 维度管理
     * @return 维度管理
     */
    @Override
    public List<MdMedDim> selectMdMedDimList(MdMedDim mdMedDim)
    {
        return mdMedDimMapper.selectMdMedDimList(mdMedDim);
    }

    /**
     * 新增维度管理
     * 
     * @param mdMedDim 维度管理
     * @return 结果
     */
    @Override
    public int insertMdMedDim(MdMedDim mdMedDim)
    {
        mdMedDim.setCdTb(mdMedDim.getCdTb().toLowerCase());
        return mdMedDimMapper.insertMdMedDim(mdMedDim);
    }

    /**
     * 修改维度管理
     * 
     * @param mdMedDim 维度管理
     * @return 结果
     */
    @Override
    public int updateMdMedDim(MdMedDim mdMedDim)
    {
        return mdMedDimMapper.updateMdMedDim(mdMedDim);
    }

    /**
     * 批量删除维度管理
     * 
     * @param idDims 需要删除的维度管理主键
     * @return 结果
     */
    @Override
    public int deleteMdMedDimByIdDims(String idDims)
    {
        return mdMedDimMapper.deleteMdMedDimByIdDims(Convert.toStrArray(idDims));
    }

    /**
     * 删除维度管理信息
     * 
     * @param idDim 维度管理主键
     * @return 结果
     */
    @Override
    public int deleteMdMedDimByIdDim(Long idDim)
    {
        return mdMedDimMapper.deleteMdMedDimByIdDim(idDim);
    }

    @Autowired
    IBaseDWService baseDWService;

    @Override
    public AjaxResult findDimDataByTableName(String tableName) {
        MdMedDim dim = new MdMedDim();
        dim.setCdTb(tableName.toLowerCase());
        List<MdMedDim> dims = this.selectMdMedDimList(dim);
        if(dims.size()>0) {
            dim = dims.get(0);
        } else {
            return AjaxResult.error("未配置["+tableName+"]元数据信息");
        }
        String flds = dim.getFlds();
        String newFlds="";
        for (String fld : flds.split(",")) {
            if(dim.getCdFld().equals(fld)) {
                newFlds+=fld+" as cd,";
            } else if(dim.getNaFld().equals(fld)) {
                newFlds+=fld+" as na,";
            } else {
                newFlds+=fld+",";
            }
        }
        List<Map<String,Object>> data =  baseDWService.selectDataByTableName(tableName,newFlds.substring(0,newFlds.length()-1));
        return AjaxResult.success(data);
    }
}
